package poker;

import java.util.LinkedList;
import java.util.Random;

/**
 * This class represents the deck.
 * Deck is designed as a stack of cards.
 * @author guglielmo90
 */
public class Deck {
    
    private LinkedList<Card> cards;
    private static int cardsnumber = 52;
    
    public Deck(){
    
        this.cards = new LinkedList<Card>();
        
        for(int i = 0 ; i < 4; i++)
        
            for (int j = 1; j < 14; j++)
            
                this.cards.add(new Card(j, i));
                 
        
    }
    
    public Card getCard(){
    
        return cards.pop();
        
    }
    
    public boolean empty(){
    
        return(cards.isEmpty());
    
    }
    
    public void swap(int i, int j){
        
        Card temp1, temp2;
        
        if(i != j) {
            
            temp1 = cards.get(i);
            temp2 = cards.get(j);
            cards.remove(i);
            cards.add(i, temp2);
            cards.remove(j);
            cards.add(j, temp1);
        
        }
        
    }
    
    /**
     * Shuffles randomly the deck.
     */
    public void shuffle(){
    
        Random ran = new Random();
        int i,j;
        
        for(int k = 0 ; k < 500 ; k++){
        
            i = Math.abs((ran.nextInt())%cardsnumber);
            j = Math.abs((ran.nextInt())%cardsnumber);
            
            swap (i,j);
            
        }
                 
    }
    
    @Override
    public String toString(){
    
        return (cards.toString());
    
    }
    
}
